<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>In-depth Explanation</title>
	<link href="../css/trailblazer_main.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="main">
  <div class="trail">
    <div class="numbox">3</div>
    <h2>What happens in the registration process?</h2>
    <img src="img/header_line.gif" />
    
       <form>
          <input type="button" value="Close Window" onclick="window.close()"/>
       </form>

    <p>
       The register.xhtml JSF form captures the user input for new account
       registration. It validates the user input, binds the data to a 
       Seam data component, and invokes a Seam UI event handler method
       when the form is submitted.
    </p>
    
<code class="block">
&lt;s:validateAll&gt;
&lt;div class="entry"&gt;
    &lt;div class="label"&gt;
        &lt;h:outputLabel for="username">Username:&lt;/h:outputLabel&gt;
    &lt;/div&gt;
    &lt;div class="input"&gt;
        &lt;h:inputText id="username" value="#{user.username}"/&gt;&lt;br/&gt;
        &lt;span class="errors"&gt;&lt;h:message for="username" /&gt;&lt;/span&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class="entry"&gt;
    &lt;div class="label"&gt;
        &lt;h:outputLabel for="name">Real Name:&lt;/h:outputLabel&gt;
    &lt;/div&gt;
    &lt;div class="input"&gt;
        &lt;h:inputText id="name" value="#{user.name}" /&gt;&lt;br/&gt;
        &lt;span class="errors">&lt;h:message for="name" /&gt;&lt;/span&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;/s:validateAll&gt;

... ...

    &lt;div class="input"&gt;
        &lt;h:commandButton value="Register" 
                         action="#{register.register}" 
                         class="button"/&gt;
        &lt;h:commandButton value="Cancel" action="login" class="button"/&gt;
    &lt;/div&gt;
</code>

   <p>
      The form fields are bound to properties of a Seam component named
      <code>user</code> via JSF EL value binding expressions such as
      <code>#{user.username}</code>. The form submit button is bound
      to the <code>register()</code> method of the Seam component named
      <code>register</code> using the JSF method binding expression 
      <code>#{register.register}</code>.
   </p>

   <p>
       Notice that the input fields are enclosed by a <code>&lt;s:validateAll</code>
       tag. This tag is part of Seam's extension to JSF. It tells the Seam
       runtime to validate those input fields when the form is submitted.
       The validation conditions are specified on the entity bean classes
       those input fields map to (e.g., here the validation condition is
       on the <code>User</code> class, see later).
       This JSF form also includes <code>&lt;h:message&gt;</code> tags that
       will display the results of any JSF validation failures.
    </p>

   <p>
      The <code>User</code> class is an EJB 3.0 entity bean with a 
      <code>@Name</code> annotation that binds the bean instance to a
      context variable named <code>user</code>. In addition to the standard 
      EJB 3.0 O/R mapping metadata, this bean features several Hibernate 
      Validator annotations such as 
      <code>@NotNull</code>, <code>@Length</code>. Due to the 
      <code>&lt;s:validateAll</code> tag in the form, these constraints are 
      automatically validated by Seam when the form is submitted. If the user 
      enters invalid data in the JSF form, the form will be redisplayed with 
      error messages.
   </p>

<code class="block">
@Entity
@Name("user")
@Scope(SESSION)
public class User implements Serializable
{
   private String username;
   private String password;
   private String name;
   
   public User(String name, String password, String username)
   {
      this.name = name;
      this.password = password;
      this.username = username;
   }
   
   public User() {}

   @NotNull
   @Length(max=100)
   public String getName()
   {
      return name;
   }
   public void setName(String name)
   {
      this.name = name;
   }
   
   @NotNull
   @Length(min=5, max=15)
   public String getPassword()
   {
      return password;
   }
   public void setPassword(String password)
   {
      this.password = password;
   }
   
   @Id
   @Length(min=5, max=15)
   public String getUsername()
   {
      return username;
   }
   public void setUsername(String username)
   {
      this.username = username;
   }
   
}</code>

    <p>
       <code>RegisterAction</code> is an EJB 3.0 stateful session bean 
       bound to the Seam context variable named <code>register</code>. 
       The <code>register()</code> method is invoked  when the form is 
       submitted.
    </p>

<code class="block">
@Stateful
@Scope(EVENT)
@Name("register")
public class RegisterAction implements Register
{

   @In
   private User user;
   
   @PersistenceContext
   private EntityManager em;
   
   @In(create=true)
   private transient FacesMessages facesMessages;
   
   private String verify;
   
   public String register()
   {
      if ( user.getPassword().equals(verify) )
      {
         List existing = em.createQuery("select username from User " + 
                                        "where username=:username")
            .setParameter("username", user.getUsername())
            .getResultList();
         if (existing.size()==0)
         {
            em.persist(user);
            return "login";
         }
         else
         {
            facesMessages.add("username #{user.username} already exists");
            return null;
         }
      }
      else 
      {
         facesMessages.add("re-enter your password");
         verify=null;
         return null;
      }
   }

   public String getVerify()
   {
      return verify;
   }

   public void setVerify(String verify)
   {
      this.verify = verify;
   }
   
   @Destroy @Remove
   public void destroy() {}
}</code>

    <p>
       The <code>@In</code> annotations inject Seam components into the <code>RegisterAction</code>
       bean. The <code>user</code> component is our entity bean, of course. The <code>facesMessages</code> 
       component is a built-in Seam component that makes it very easy to display templated and localized
       messages to the user, even when redirect after post is used.
    </p>

       <form>
          <input type="button" value="Close Window" onclick="window.close()"/>
       </form>

  </div>
</div>

</body>
</html>
